<?php

namespace App\Http\Controllers\MiniAPP;

use App\Http\Controllers\Controller;
use App\Http\Resources\MiniAPP\StoreResource;
use App\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StoreController extends Controller
{
    public function getList(Request $request)
    {
        $lng = $request->lng;
        $lat = $request->lat;
        $name = $request->name;

        $stores = Store::where('is_online', false);

        if (!is_null($lng) && !is_null($lat)) {

            //按照经纬度排序
            $stores = $stores->selectRaw('*, st_distance(POINT(`lng`, `lat`), POINT(?, ?)) * 111195 / 1000 as distance', [
                $lng, $lat
            ])
                ->orderBy('distance', 'asc');
        }

        if (!is_null($name)) {
            $stores = $stores->where('name', 'like', '%' . $name . '%');
        }

        $stores = $stores->get();

        return StoreResource::collection($stores);
    }
}
